<?php
class BizmanageAction extends CommonAction {
	//过滤查询字段
	
	function _filter(&$map){
		if ($_POST['name']=='请输入关键字')$_POST['name']='';
		$map['name'] = array('like',"%".$_POST['name']."%");
	}
    /**
     +----------------------------------------------------------
     * 区域列表
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function zone()
    {
        //读取系统的用户列表
        $zone    =   D("Zone");
		$list=$zone->field('id,name')->select();
		
		//dump(	$user);
		foreach ($list as $vo){
			$userList[$vo['id']]	=	$vo['name'];
		}


        $this->assign('actionList',$userList);
        $this->display();

        return;
    }
    public function comindex()
    {
        //读取系统的用户列表
        $company    =   D("Bizmanage");
       $list=$company->where('status=1')->field('id,name,create_time,status,contact,tel,water,electricity,property,wastewater')->select();
            
		//dump(	$user);
		// 设计服务开通标识
        $this->assign('list',$list);
        $this->display();

        return;
    }
	//获得区域列表
    public function commerceadd(){
		$zone =D("Zone");
		$pId =  isset($_GET['pid'])?$_GET['pid']:1;
		$id	=	$zone->getField("id");
		$where['status']=1;
		$where['pid']=$pId;
		$list	=	$zone->where($where)->field('id,name')->select();
		
		foreach ($list as $vo){
			$zonesList[$vo['id']]	=	$vo['name'];
		}
		//echo json_encode($zonesList); 
		 //$this->ajaxReturn($zonesList);
               $setting['water']= C('WATER');
               $setting['electricity']= C('ELECTRICITY');
               $setting['property']= C('PROPERTY');
               $setting['wastewater']= C('WASTWATER');
               $setting['heating']= C('HEATING');
                $this->assign('setting',$setting);
                $this->assign('lista',$zonesList);
                $this->display();
		return;
	}
//添加用户商网信息

    public function placeadd(){
                $model = D('BizmanageView');
                $id =  isset($_GET['id'])?$_GET['id']:1;
                $com = $model->getById($id);
               
                $this->assign('com', $com);
                //创建数据对象
                $pid =  isset($_GET['pid'])?$_GET['pid']:$com['zoneid'];
                $zone = D("ZoneView");
                $where['status']=1;
                $where['ztype']=1;
                $where['pid']=$pid;
                $where['bizid']=0;
		//全部列表
                $listall	=	$zone->where($where)->field('id,name,place')->select();

                //$where['bizid']=$com['id'];
                //已有列表
		//$list	=	$zone->where($where)->field('id,name,place')->select();

                //$this->assign('userList',$list);
                $this->assign('zonesList',$listall);
                $this->display();
		return;
	}        
public function placemanage(){
                $model = D('BizmanageView');
                $id =  isset($_GET['id'])?$_GET['id']:1;
                $com = $model->getById($id);
               
                $this->assign('com', $com);
                 //创建数据对象
                $pid =  isset($_GET['pid'])?$_GET['pid']:$com['zoneid'];
                $zone = D("ZoneView");
                $where['status']=1;
                $where['ztype']=1;
                $where['pid']=$pid;
                if (!empty($zone)) {
                    $this->_list($zone, $where,'id',false,'id='.$id.'&');
                }

                Cookie::set('_currentUrl_', __SELF__);

                $this->display();
		return;
	}        
    public function getZoneList(){
		
		$zone =D("Zone");
		$pId =  isset($_GET['pid'])?$_GET['pid']:1;
		$where['status']=1;
		$where['pid']=$pId;
                                    $where['bizid']=0;
		$zonesList	=	$zone->where($where)->field('id,name,place')->select();
		
		//foreach ($list as $vo){
		//	$zonesList[$vo['id']]	=$vo['name'];
		//}
		//echo json_encode($zonesList); 
		$this->ajaxReturn($zonesList);
	}
    public function insertBiz() {
                if (isset($_POST['zoneid'])) { 
                    $data['zoneid']=0;
                 }
                $data['name']  =  $_POST['name'];
                $str  = substr(Trim($_POST['ids']),0,strlen(Trim($_POST['ids']))-1);
                $data['storenum'] =sizeof(explode(',', $str));
                $data['tel'] = $_POST['tel'];
		$data['znum']  = $_POST['znum'];
                $data['znum2']  = $_POST['znum2'];
		$data['remark'] =$_POST['remark'];
		$data['address'] =$_POST['address'];
		$data['contact'] = $_POST['contact'];
                $data['w_price']= $_POST['water'];
                $data['e_price']= $_POST['electricity'];
                $data['p_price']= $_POST['property'];
                $data['ww_price']= $_POST['wastewater'];
                $data['h_price']= $_POST['heating'];
                $data['zoneid']= $_POST['zoneid'];
                $checkbox = $_POST['sample-checkbox'];
                                    foreach ($checkbox as $vo){
                                        switch ($vo){
                                            case 1 :
                                                $data['water'] =1;
                                                break;
                                            case 2 :
                                                $data['electricity'] =1;
                                                break;
                                            case 3:
                                                $data['property'] =1;
                                                break;
                                            case 4 :
                                                $data['wastewater'] =1;
                                                break;
                                            case 5:
                                                $data['heating'] =1;
                                                break;
                                        }
                                        
                                    }

		
		$Biz = D("Bizmanage");
		if(!$Biz->create($data)) {
			$this->error($Biz->getError());
		}else{
			// 写入帐号数据
			if($result	 =$Biz->add()) {
                            //目录里计数
                                $num=$Biz->addBizNum($data['zoneid']);
                                //缓存商网统计
                                $Biz->cacheBiz() ;
                            //更新用户SID;
                                $strSID=sprintf( "%04d ",$num);
                                
                                $Biz->setSID($result,sprintf("%02d ",$data['zoneid']).$strSID);
				$Biz->setBizZoneActions($result,$str);
                                //添加记录统计记录
                                $Biz->setMeterStatistics($result);
				$this->assign('jumpUrl', Cookie::get('_currentUrl_'));
				$this->success('添加新商户成功！');
			}else{
				$this->error('添加新商户失败！');
			}
		}
        


    }
    public function index(){
        $biz = D("Bizmanage");
        $pid= isset($_GET['zoneid'])?$_GET['zoneid']:0;
        $name= isset($_REQUEST['name'])?$_REQUEST['name']:0;
        $zlist=isset($_POST['$zlist'])?$_GET['$zlist']:0;
        if ($pid!=0){
                $where['zoneid']=$pid; 
        }
        $where['status']=1;
                 $name= isset($_REQUEST['name'])?$_REQUEST['name']:0;
         
         $zlist=isset($_REQUEST['zlist'])?$_REQUEST['zlist']:0;
         $tel=isset($_REQUEST['tel'])?$_REQUEST['tel']:0;
         $contact=isset($_REQUEST['contact'])?$_REQUEST['contact']:'';
         //请输入商户名或者编码
         if(($name!='请输入商户名或者编码')&&($name!='')){
                //输入为用户编号
                if (is_numeric($name)){
                      $where['bizSid']= array('like','%'.$name.'%');
                }else{
                    //输入为用户名
                   $where['name']= array('like','%'.$name.'%');
                }
             
         }
         if($zlist>0){
              $where['zoneid']= $zlist;
         }
         if($tel>0){
             $where['tel']= $tel;
         }
         if($contact!=''){
             $where['contact']= array('like','%'.$contact.'%');
         }
          //高级查询组合
        
        
        
        
        if (!empty($biz)) {
            $this->_list($biz, $where,'id',true);
        }
        
        $zones = F('zones');

        foreach ($zones as $vo){
             //设置选择区域     
             if($vo['id']==$pid){
                       $this->assign('zonestitle',$vo['name'] );
                       $vo['selected']=1;
                   }else{
                       $vo['selected']=0;
                   }
          }
          
        $this->assign('zones',$zones );
        Cookie::set('_currentUrl_', __SELF__);
        $this->display();
        return;
    }
    //表信息显示公司列表
    public function meterclist(){
        $biz = D("Bizmanage");
        $pid= isset($_GET['zoneid'])?$_GET['zoneid']:0;
        $name= isset($_REQUEST['name'])?$_REQUEST['name']:0;
        $zlist=isset($_POST['$zlist'])?$_GET['$zlist']:0;
        if ($pid!=0){
                $where['zoneid']=$pid; 
        }
        $where['status']=1;
                 $name= isset($_REQUEST['name'])?$_REQUEST['name']:0;
         
         $zlist=isset($_REQUEST['zlist'])?$_REQUEST['zlist']:0;
         $tel=isset($_REQUEST['tel'])?$_REQUEST['tel']:0;
         $contact=isset($_REQUEST['contact'])?$_REQUEST['contact']:'';
         //请输入商户名或者编码
         if(($name!='请输入商户名或者编码')&&($name!='')){
                //输入为用户编号
                if (is_numeric($name)){
                      $where['bizSid']= array('like','%'.$name.'%');
                }else{
                    //输入为用户名
                   $where['name']= array('like','%'.$name.'%');
                }
             
         }
         if($zlist>0){
              $where['zoneid']= $zlist;
         }
         if($tel>0){
             $where['tel']= $tel;
         }
         if($contact!=''){
             $where['contact']= array('like','%'.$contact.'%');
         }
          //高级查询组合
        
        
        
        
        if (!empty($biz)) {
            $this->_list($biz, $where,'id',true);
        }
        
        $zones = F('zones');
         foreach ($zones as $vo){
             //设置选择区域     
             if($vo['id']==$pid){
                       $this->assign('zonestitle',$vo['name'] );
                       $vo['selected']=1;
                   }else{
                       $vo['selected']=0;
                   }
          }
          
        $this->assign('zones',$zones );
        Cookie::set('_currentUrl_', __SELF__);
        $this->display();
        return;
    }    
 
        public function edit(){
                $name = $this->getActionName();
                $model = D($name);
                $id = $_REQUEST [$model->getPk()];

                $com = $model->getById($id);
                
                $this->assign('com', $com);
                
                $mPlace=$model->getBizZoneActions($com['id'],$com['zoneid']);
                foreach ($mPlace as $vo){
                    $str=$vo[id].','.$str;
                }
                $str = substr(Trim($str),0,strlen($str)-1);
       //获取地区列表
        $zone =D("Zone");
	$pId =  isset($_GET['pid'])?$_GET['pid']:1;
	$id	=	$zone->getField("id");
	$where['status']=1;
	$where['pid']=$pId;
	$list	=	$zone->where($where)->field('id,name')->select();
		
	foreach ($list as $vo){
		$zonesList[$vo['id']]	=	$vo['name'];
	}
                 

        
           $this->assign('meterlist',$meterlist);
                //$this->assign('myzonesList',$myzonesList);
                $this->assign('mPlaceList',$mPlace);
                $this->assign('zonesList',$zonesList);
                $this->assign('selectid',$com['zoneid']);
                $this->assign('ids',$str);
                 
                $this->display();
        } 
        //添加表信息
        public function editMeter(){
            $Id =  $_GET['bid'];
             $zone =D("Zone");
             $where['status']=1;
            $where['bizid']=$Id;
            $list=$zone->where($where)->field('id,name')->select();
		
	foreach ($list as $vo){
		$zonesList[$vo['id']]	=	$vo['name'];
	}  
            $this->assign('bid',$Id);
            $this->assign('zonesList',$zonesList);
             $this->display();
        }
        public function getMeterInfo(){
                 $Id =  $_GET['bid'];
                 $company    =   D("Bizmanage");
                 $list= $company->getBizMeterActions($Id) ;
                 $this->ajaxReturn($list);
        }
        public function insertMeterInfo(){
                 $data['zoneid']  =  $_POST['zoneid'];
                 $id=$_POST['id'];
                 $data['bizid']  =  $id;
                  $data['metertype'] = $_POST['metertype'];
                  $data['m_price'] = $_POST['m_price'];
                 $data['meterid'] =$_POST['meterid'];
                 $company    =   D("Bizmanage");
                 $list= $company->addBizMeterInfo($data) ;
                   if (false !== $list) {
                //成功提示
                $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
                    $this->success('编辑成功!');
                    } else {
                    //错误提示
                    $this->error('编辑失败!');
                 }      
            return;
        }
        public function stop(){
            //删除指定记录

            $model = D("Bizmanage");
            if (!empty($model)) {
                $pk = $model->getPk();
                $id = $_REQUEST [$pk];
                if (isset($id)) {
                    $condition = array($pk => array('in', explode(',', $id)));
                    $list = $model->where($condition)->setField('status', - 1);
                    if ($list !== false) {
                        $this->success('删除成功！');
                    } else {
                        $this->error('删除失败！');
                    }
                } else {
                    $this->error('非法操作');
                }
        }    
            
        }
        public function updateBiz(){
            $step=$_POST['step'];
            $name = $this->getActionName();
            $model = D($name);
             if (false === $model->create()) {
                $this->error($model->getError());
             }
        // 更新数据
 
            switch ($step){
                //更新基本信息b部分
                case 1:
                        
                     break;
                //更新地区信息部分
                case 2:
                    $model->setBizZoneActions($data['id'],$data['ids']); 
                    $data['storenum'] =sizeof(explode(',',$data['ids']));
                    break;
                //更新服务及单价部分
                case 3:
                        break;
                   
                
            }
            $list = $model->save();
              if (false !== $list) {
                //成功提示
               $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
                $this->success('编辑成功!');
            } else {
            //错误提示
             $this->error('编辑失败!');
        }      
            return;
        }
        public function addMeter(){
             $model = D('BizmanageView');
             $id =  isset($_GET['id'])?$_GET['id']:1;
             $com = $model->getById($id);
               
              $this->assign('com', $com);
            
            $this->display();
            return;
        }
        public function  indexMeter(){
                $model = D('BizMeterView');
                $id =  isset($_GET['id'])?$_GET['id']:1;

                $where['bizid']=$id;
                if (!empty(  $model )) {
                    $this->_list( $model , $where,'id',false,'id='.$id.'&');
                }

                Cookie::set('_currentUrl_', __SELF__);

                $this->display();
		return;

            
        }
        public function  meterList(){
                
                $biz = D('Bizmanage');
                $id = $_REQUEST [$biz ->getPk()];
                $this->assign('REFERER', $_SERVER['HTTP_REFERER']);
                $com = $biz->getById($id);
                
                $this->assign('com', $com);
            
                $model = D('BizMeter');
                $id =  isset($_GET['id'])?$_GET['id']:1;

                $where['bizid']=$id;
                if (!empty(  $model )) {
                    $list	=	$model->where($where)->select();
                }
                for($i=0;$i<sizeof($list);$i++)
                {
                    if ($list[$i]['metertype']== 1){
                        $w_meterlist[]=$list[$i];
                    }else{
                        $e_meterlist[]=$list[$i];
                    }
                }
                 $this->assign('listw',$w_meterlist);
                 $this->assign('liste',$e_meterlist);
                  Cookie::set('_currentUrl_', __SELF__);
                $this->display();
		return;

            
        }
}
?>